package com.qc31.monitorlib.custom

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.os.Build
import android.text.Html
import android.text.Spanned
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.qc31.baselib.R
import com.qc31.baselib.rx.queryThrottle
import com.qc31.monitorlib.databinding.CustomLineTextViewBinding
import io.reactivex.rxjava3.core.Observable


class LineTextView : LinearLayout {
    private var rightDrawable: Drawable? = null

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView(context, attrs)
    }

    private lateinit var binding: CustomLineTextViewBinding

    @SuppressLint("CustomViewStyleable")
    private fun initView(context: Context, attrs: AttributeSet?) {
        binding = CustomLineTextViewBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.left_right_text)
        val rightWeight: Float = typedArray.getFloat(R.styleable.left_right_text_rightWeight, 2f)
        val testSize: Float = typedArray.getFloat(R.styleable.left_right_text_textSize, 14f)
        val background: Drawable? = typedArray.getDrawable(R.styleable.left_right_text_background)
        val leftGravity: Int = typedArray.getInt(R.styleable.left_right_text_leftGravity, 0)
        val rightGravity: Int = typedArray.getInt(R.styleable.left_right_text_rightGravity, 0)
        val leftText: String? = typedArray.getString(R.styleable.left_right_text_leftText)
        val rightText: String? = typedArray.getString(R.styleable.left_right_text_rightText)
        val rightHint: String? = typedArray.getString(R.styleable.left_right_text_rightHint)
        val leftTextColor: Int = typedArray.getColor(
            R.styleable.left_right_text_leftTextColor,
            ContextCompat.getColor(context, R.color.color_333)
        )
        val rightTextColor: Int = typedArray.getColor(
            R.styleable.left_right_text_rightTextColor,
            ContextCompat.getColor(context, R.color.color_666)
        )
        rightDrawable =
            typedArray.getDrawable(R.styleable.left_right_text_rightDrawable)
        val isLineChange = typedArray.getBoolean(R.styleable.left_right_text_isLineChange, false)
        val isLineGone = typedArray.getBoolean(R.styleable.left_right_text_isLineGone, false)
        when (leftGravity) {
            0 -> binding.tvLeft.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            1 -> binding.tvLeft.gravity = Gravity.CENTER
            2 -> binding.tvLeft.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        }
        when (rightGravity) {
            0 -> binding.tvRight.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            1 -> binding.tvRight.gravity = Gravity.CENTER
            2 -> binding.tvRight.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        }
        if (background != null)
            this.background = background
        binding.tvRight.textSize = testSize
        binding.tvLeft.textSize = testSize

        val params = LayoutParams(0, LayoutParams.MATCH_PARENT)
        params.weight = rightWeight
        binding.tvRight.layoutParams = params
        rightDrawable?.apply {
            setBounds(0, 0, minimumWidth, minimumHeight)
            binding.tvRight.setCompoundDrawables(null, null, this, null)
        }

        binding.tvLeft.setTextColor(leftTextColor)
        binding.tvRight.setTextColor(rightTextColor)
        binding.tvLeft.text = leftText ?: ""
        binding.tvRight.text = rightText ?: ""
        binding.tvRight.hint = rightHint ?: ""

        if (isLineChange) {
            binding.bottomLine.visibility = View.GONE
            binding.verticalLine.visibility = View.GONE
            binding.bottomLine12.visibility = View.VISIBLE
            binding.verticalLine16.visibility = View.VISIBLE
        }
        if (isLineGone) {
            binding.bottomLine.visibility = View.GONE
        }
        typedArray.recycle()
    }

    fun setLeftTitle(leftText: String?) {
        binding.tvLeft.text = leftText
    }

    fun setLeftTitle(leftTextId: Int) {
        binding.tvLeft.setText(leftTextId)
    }

    fun setRightText(rightText: String?) {
        binding.tvRight.text = rightText
    }

    fun setRightText(rightText: Spanned) {
        binding.tvRight.text = rightText
    }

    fun setRightHint(righthint: String?) {
        binding.tvRight.hint = righthint
    }

    fun setRightHint(righthintId: Int) {
        binding.tvRight.setHint(righthintId)
    }

    fun setRightText(rightTextId: Int) {
        binding.tvRight.setText(rightTextId)
    }

    fun getRightText(): String {
        return binding.tvRight.text.toString()
    }

    fun rightClick(): Observable<Unit> {
        return binding.tvRight.queryThrottle()
    }

    fun rightTextColor(color: Int) {
        binding.tvRight.setTextColor(ContextCompat.getColor(context, color))
    }

    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        binding.tvRight.isEnabled = isEnabled
    }

    fun setStartTitle(startText: String) {
        val htmlStr = "<font color='#111111'>$startText</font><font color='#FF0000'>*</font>"
        binding.tvLeft.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(htmlStr, Html.FROM_HTML_MODE_LEGACY)
        } else {
            Html.fromHtml(htmlStr)
        }
    }

    fun nextVisible(isVisible: Boolean) {
        if (isVisible) {
            rightDrawable?.apply {
                setBounds(0, 0, minimumWidth, minimumHeight)
                binding.tvRight.setCompoundDrawables(null, null, this, null)
            }
        } else {
            binding.tvRight.setCompoundDrawables(null, null, null, null)

        }

    }

}